// pages/medical/register/index.js
import {
  createPatient
} from "../../../utils/hospital-api";
import formRules from "../../../utils/rules-form"
const rules = {
  username: [{
    required: true,
    message: '请输入用户名'
  }, {
    pattern: /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[@.])[a-zA-Z0-9@.]{6,18}$/,
    message: '用户名格式错误'
  }],
  password: [{
    required: true,
    message: '请输入密码'
  }, {
    pattern: /^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*()_+-=:,.?{}])/,
    message: '密码格式错误'
  }],
  comfirmPassword: [{
    required: true,
    message: '请输入确认密码'
  }],
  phone: [{
    required: true,
    message: '请输入手机号'
  }, {
    pattern: /^1[3-9]\d{9}$/,
    message: '手机号格式错误'
  }]
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    form: {
      username: '',
      password: '',
      comfirmPassword: '',
      name: wx.getStorageSync('userInfo').name,
      idNum: wx.getStorageSync('userInfo').idNumber,
      phone: '',
      flagstat: 2
    },

  },
  onInput(e) {
    let str = 'form.' + e.target.dataset.key
    this.setData({
      [str]: e.detail
    })
  },
  // 注册南京12320平台的用户
  register() {
    formRules(rules, this.data.form, (status) => {
      if (status) {
        if (this.data.form.password !== this.data.form.comfirmPassword) {
          wx.showToast({
            title: '密码和确认密码不一致',
            icon: 'none'
          })
          return;
        }
        createPatient({
          data: JSON.stringify({
            username: this.data.form.username,
            password: this.data.form.password,
            name: wx.getStorageSync('userInfo').name,
            idNum: wx.getStorageSync('userInfo').idNumber,
            phone: this.data.form.phone,
            flagstat: this.data.form.flagstat,
          })
        }).then(res => {
          if (res.data.SUCCESSFUL == 1) {
            wx.showToast({
              title: '成功',
              icon: 'success',
              duration: 2000,
              success() {
                wx.navigateBack()
              }
            })
          } else {
            wx.showModal({
              title: '注册失败',
              content: res.data.MSG,
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                }
              }
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})